package com.jp.vn.CustomAdapter;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.jp.vn.main.R;
import com.jp.vn.Object.Category;
import com.jp.vn.Object.SoDem;

public class SoDemAdapter extends BaseExpandableListAdapter {

	Context context;
	ArrayList<Category> arrayCategoryNum;
	LayoutInflater inflater;
	MediaPlayer mediaPlayer = null;
	Typeface type;

	public SoDemAdapter(Context context, ArrayList<Category> arrayCategoryNum) {
		this.context = context;
		this.arrayCategoryNum = arrayCategoryNum;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		type = Typeface
				.createFromAsset(context.getAssets(), "fonts/tekton.ttf");
	}

	@Override
	public int getGroupCount() {
		return arrayCategoryNum.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return arrayCategoryNum.get(groupPosition).getArraySoDem().size();
	}

	@Override
	public int getChildTypeCount() {
		return 3;
	}

	@Override
	public int getChildType(int groupPosition, int childPosition) {
		int result = 0;
		if (groupPosition == 5) {
			return 1;
		}
		if (groupPosition == 4) {
			return 2;
		}
		return result;
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View v = convertView;

		if (v == null) {
			v = inflater.inflate(R.layout.group, parent, false);
		}

		v.setMinimumHeight((int) context.getResources().getDimension(
				R.dimen.item_num_height));

		TextView txtName = (TextView) v.findViewById(R.id.tfName);
		txtName.setText(arrayCategoryNum.get(groupPosition).getName());
		ImageView imgarrow = (ImageView) v.findViewById(R.id.ic_arrow);

		int ic_arrow = isExpanded ? R.drawable.open : R.drawable.close;
		imgarrow.setBackgroundResource(ic_arrow);

		v.setBackgroundColor(groupPosition % 2 == 0 ? Color
				.parseColor("#f75f8c") : Color.parseColor("#f979a0"));
		
		if (!isExpanded) {
			stopPlaying();
		}

		return v;
	}
	
	public class ViewHolder{
		TextView tfNumJp, tfNumVn, tfNumLattin = null;
		ImageView btnAudio;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View v = convertView;
		final ViewHolder holder;
		final ImageView btnAudio;
		SoDem num =  arrayCategoryNum.get(groupPosition).getArraySoDem().get(childPosition);
		
		if (v == null) {
		  holder = new ViewHolder();
			switch (groupPosition) {
			case 0:case 1:case 2:case 3:
				v = inflater.inflate(R.layout.item_num3, parent, false);
				holder.tfNumVn = (TextView) v.findViewById(R.id.tfSoDem);
				holder.tfNumJp = (TextView) v.findViewById(R.id.tfJp_Num);
				holder.tfNumLattin = (TextView) v.findViewById(R.id.tfLatin_Num);
				v.setTag(holder);
				break;
			case 4:
				v = inflater.inflate(R.layout.item_num, parent, false);
				holder.tfNumVn = (TextView) v.findViewById(R.id.tfSoDem);
				holder.tfNumJp = (TextView) v.findViewById(R.id.tfJp_Num);
				holder.tfNumLattin = (TextView) v.findViewById(R.id.tfLatin_Num);
				v.setTag(holder);
				break;
			case 5:
				v = inflater.inflate(R.layout.item_num2, parent, false);
				holder.tfNumVn = (TextView) v.findViewById(R.id.tfSoDem);
				holder.tfNumJp = (TextView) v.findViewById(R.id.tfJp_Num);
				v.setTag(holder);
				break;
			}
		}
		else{
			holder = (ViewHolder)v.getTag();
		}


		btnAudio = (ImageView)v.findViewById(R.id.imgAudio);
		holder.tfNumVn.setText(num.getNumVn());
		holder.tfNumJp.setText(num.getNumJp());
		
		if (groupPosition != 5) {
			holder.tfNumLattin.setText(num.getNumRomanji());
		}

		if (groupPosition % 2 == 0) {
			v.setBackgroundColor(childPosition % 2 == 0 ? Color
					.parseColor("#fdbed1") : Color.parseColor("#fbe0e8"));
		} else {
			v.setBackgroundColor(childPosition % 2 == 0 ? Color
					.parseColor("#fbe0e8") : Color.parseColor("#fdbed1"));
		}

		v.setMinimumHeight((int) context.getResources().getDimension(
				R.dimen.num_height));

		btnAudio.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					stopPlaying();
					playSound(holder.tfNumVn.getText().toString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		return v;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	public void playSound(String fileName) throws IOException {
		AssetFileDescriptor afd = context.getAssets().openFd(
				"sodem/" + fileName + ".ogg");
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
		mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mp.start();
			}
		});

		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp) {
						mp.release();
						mediaPlayer = null;
					}
				});

		mediaPlayer.prepareAsync();
	}
	
	public void stopPlaying(){
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
}
